﻿using MyProduce.MyProduce.ProduceDto;
using MyProduce.Produce;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyProduce.MyProduce
{
    /// <summary>
    /// 羊只转舍
    /// </summary>
    public class RescissionAppService : ApplicationService, IRescissionAppService
    {

        private readonly IRepository<Camp,int> _campRepository;

        private  readonly IRepository<RoundType, int> _produceRepository;
        public RescissionAppService(IRepository<Camp, int> campRepository, IRepository<RoundType, int> produceRepository)
        {
            _campRepository = campRepository;
          
            _produceRepository = produceRepository;
        }
        /// <summary>
        /// 添加羊只转舍
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<EarResult<CampDto>> CampAdd(Camp input)
        {
            Camp camp = new Camp();
            camp.Transfertobuilding = input.Transfertobuilding;
            camp.Variety = input.Variety;
            camp.Surrenderdate = input.Surrenderdate;
            camp.Cottage = input.Cottage;
            camp.Field = input.Field;
            camp.Cottage = input.Cottage;
            camp.Field = input.Field;
            camp.Sex = input.Sex;
            camp.Overbit = input.Overbit;
            camp.Drivefield = input.Drivefield;
            camp.Surrenderdate = input.Surrenderdate;
            camp.Roundtype = input.Roundtype;
            var result = await _campRepository.InsertAsync(camp);
            var res = ObjectMapper.Map<Camp, CampDto>(result);
            if (res != null)
            {
                return new EarResult<CampDto>() { Code = 200, Message = "添加成功" };
            }
            else
            {
                return new EarResult<CampDto>() { Code = 500, Message = "添加失败" };
            }
        }
        /// <summary>
        /// 羊只转舍删除
        /// </summary>
        /// <param name="Aid"></param>
        /// <returns></returns>
        public async Task DeleteCamp(int Aid)
        {
            await _campRepository.DeleteAsync(Aid);  
        }

        public async Task<CampDto> GetCampFindaa(int eid)
        {
            var result= await _campRepository.GetAsync(eid);
            return ObjectMapper.Map<Camp, CampDto>(result);
        }
        /// <summary>
        /// 获取羊只转舍列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<CampDto>> GetCampList(string? Overbit, string? Transferbuilding, string? Drivefield,string ? Roundtype)
        {

            var result = await _campRepository.GetListAsync();
            var list = (from r in result
                        select new CampDto
                        {
                            Id = r.Id,
                            Transfertobuilding = r.Transfertobuilding,
                            Variety = r.Variety,
                            Surrenderdate = r.Surrenderdate,
                            Cottage = r.Cottage,
                            Field = r.Field,
                            Sex = r.Sex,
                            Overbit = r.Overbit,
                            Drivefield = r.Drivefield,
                            Roundtype = r.Roundtype,
                        }
                      ).ToList();
            if (Overbit!= null)
            {
                list = list.Where(r => r.Overbit.Contains(Overbit)).ToList();
            }
            if (Transferbuilding!= null)
            {
                list = list.Where(r => r.Transfertobuilding.Contains(Transferbuilding)).ToList();
            }
            if (Drivefield!= null)
            {
                list = list.Where(r => r.Drivefield.Contains(Drivefield)).ToList();
            }
            if(Roundtype!=null)
            {
                list=list.Where(x=>x.Roundtype.Contains(Roundtype)).ToList();
            }
            return list;
        }
        /// <summary>
        /// 转舍类型下拉显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoundTypeDto>> GetRountList()
        {
            var result = await _produceRepository.GetListAsync();
            var list = (from r in result
                        select new RoundTypeDto
                        {
                            RoundName = r.RoundName,
                            Id = r.Id,

                        }
                      ).ToList();
            return list;
        }
    }
}
